package com.iflytek.management.controller;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iflytek.management.mapper.ProjectMapper;
import com.iflytek.management.mapper.ProjectUserRoleMapper;
import com.iflytek.management.pojo.Project;
import com.iflytek.management.pojo.ProjectUserRole;
import com.iflytek.management.pojo.SysUser;
import com.iflytek.management.util.MybatisUtil;

@Controller
@RequestMapping("/project")
public class ProjectController { 
	
	/**
	 * 添加新项目
	 * @param record 需要传入的项目信息
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping("/add.do")
	@ResponseBody
	public ModelAndView addProject(@RequestBody Project record) throws Exception{
		ModelAndView mv = new ModelAndView();
		ProjectMapper projectMapper=MybatisUtil.Prepare(ProjectMapper.class);
		try {
			int i = projectMapper.insert(record);
			if (i == 1) {
				mv.addObject("msg","添加成功");
				mv.addObject("");
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			mv.addObject("msg", "添加失败");
		}
		return mv;
		
		
		}
	
	
	/**
	 * //通过id删除项目（假删除）
	 * @param proid 项目ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public ModelAndView deletProect(@RequestBody Integer proid ) throws Exception {
		ModelAndView mv = new ModelAndView();
		ProjectMapper projectMapper = MybatisUtil.Prepare(ProjectMapper.class);
		try {
			int i = projectMapper.deleteByPrimaryKey(proid);
			if (i==1) {
				//虚假删除
				mv.addObject("msg","删除成功");
				
			}
		} catch (Exception e) {
			System.out.println(e);
			mv.addObject("msg","删除失败");
			
		}
		return mv;}
	
	
	/**
	 * 查询项目所有信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/find.do")
	@ResponseBody
	public ModelAndView findProject() throws Exception {
		ModelAndView mv = new ModelAndView();
		ProjectMapper projectMapper=MybatisUtil.Prepare(ProjectMapper.class);
		try {
			List<Project> projects = projectMapper.selectAll();
			mv.addObject("project",projects);
			mv.addObject("msg","查询成功");
		} catch (Exception e) {
			mv.addObject("msg","查询失败");
			
		}
		return mv;
		
	}
	
	/**
	 * 更新项目信息
	 * @param projectList 项目信息同时带有项目成员
	 * @return 成功或失败
	 * @throws Exception
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public ModelAndView updateProjectByid(@RequestBody List<Project> projectList ) throws Exception {
		ModelAndView mv = new ModelAndView();
		ProjectMapper projectMapper=MybatisUtil.Prepare(ProjectMapper.class);
		ProjectUserRoleMapper projectUserRoleMapper=MybatisUtil.Prepare(ProjectUserRoleMapper.class);
		Project project = new Project();
		ProjectUserRole projectUserRole=new ProjectUserRole();
		for (Project simProject : projectList) {
			Integer proid=simProject.getProid();
			project.setProid(proid);
			String proname=simProject.getProname();
			project.setProname(proname);
			Date begindate=simProject.getBegindate();
			project.setBegindate(begindate);
			Date enddate=simProject.getEnddate();
			project.setEnddate(enddate);
			String remark=simProject.getRemark();
			project.setRemark(remark);
			String proleader=simProject.getProleader();
			project.setProleader(proleader);
			Integer iddelete=simProject.getIsdelete();
			project.setIsdelete(iddelete);
			List<SysUser> sysUsersList=project.getSysUsersList();
			for (SysUser sysUser : sysUsersList) {
				Integer userid=sysUser.getUserid();
				projectUserRole.setUserid(userid);
				projectUserRole.setProid(proid);
				
				
				
			}
			
		}
		try {
			int i = projectMapper.updateProjectByid(project);
			int o = projectUserRoleMapper.updateByProid(projectUserRole);
			if (i==1&&o==1) {
				mv.addObject("msg","修改成功");
				
			}
		} catch (Exception e) {
			mv.addObject("msg","修改失败");
		}
		return mv;
		
	}
	
	
	

}
